<?php
namespace Rest\Logic;
class ArticleLogic extends CommonLogic {

    public function add($data) {
        // 标题
        $title = v($data,'title');
        verify($title,'notEmpty','文章标题为空');
        // 内容
        $content = v($data,'content');
        verify($content,'notEmpty','文章内容为空');
        $content = clearHtml($content);
        // 作者
        $author = v($data,'author');
        verify($author,'notEmpty','文章作者不能为空');
        // 分类
        $cid = v($data,'cid');
        verify($cid,'notEmpty','分类为空');
        verify($cid,'isNumber','非法分类');
        $cid = intval($cid);
        // 来源名称
        $source_name = v($data,'source_name');
        verify($source_name,'notEmpty','来源名称不能为空');
        // 来源地址
        $source_url = v($data,'source_url');
        verify($source_url,'notEmpty','来源地址为空');
        verify($source_url,'isUrl','非法来源地址');
        // 热度值
        $hotnum = v($data,'hotnum');
        verify($hotnum,'isNumber','非法热度值');
        $hotnum = intval($hotnum);
        // 中文分词
        $wordList = segment($content,'word');
        // 标签
        $tags = explode(',',v($data,'tags'));
        $tagList = array();
        foreach($tags as $tag) {
            $tag = trim($tag);
            $tag = trim($tag,',');
            if(!trim($value)) continue;
            $tagList[] = $value;
        }
          
        $tags = array_unique(array_merge($tagList,$wordList));
        !$tags && $tags = array();
        // 封面
        $cover = v($data,'cover');
        if(verify($cover,'notEmpty')) {
            verify($cover,'isUrl','非法封面');
            $imageInfo = fetchImage($cover,'article');
            $cover = $imageInfo ? $imageInfo['path'] : '';
        }
        // 发布时间
        $publishtime = v($data,'publishtime');
        verify($publishtime,'isTimestamp','非法发布时间');
        // 分类是否存在
        $CategoryModel = M('Category');
        $categoryInfo = $CategoryModel->find($cid);
        if(!$categoryInfo) {
            E('分类 ' .$cid. ' 不存在');
        }
        // 查询文章
        $ArticleMongoModel = D('ArticleMongo');
        $articleInfo = $ArticleMongoModel->where(array('title'=>$title))->find();
        // 文章已存在
        if(verify($articleInfo,'notEmpty')) {
            if(isset($articleInfo['cid'][$cid])) {
                E('文章' . $title . '已经存在');
            }
            // 直接将分类加入到存在文章的分类字段
            $articleInfo['cid'][$cid] = $cid;
            $ArticleMongoModel->save(array(
                'id'            =>  $articleInfo['id'],
                'cid'           =>  $articleInfo['cid'],
                'updatetime'    =>  NOW_TIME
            ));
            return $articleInfo['id'];
        }
        // 是否有图片
        $is_image = 0;
        // 解析文章图片
        $image_count = preg_match_all('#<img[^>]* src=[\'"]([^\'"]+)[\'"][^>]*>#',$content,$matches);
        if($image_count) {
            $is_image = 1;
            foreach($matches[0] as $key=>$str) {
                $imageInfo = fetchImage($matches[1][$key],'article');
                // 如果没有上传封面则获取的第一张图片作为封面
                !$cover && $cover = $imageInfo ? $imageInfo['page'] : '';
                !$cover && $cover = $imageInfo ? $imageInfo['page'] : '';
                // 替换文章中原来的图片
                $replace = $imageInfo ? "<img class=\"i\" data-src=\"{$imageInfo['path']}\" data-width=\"{$imageInfo['width']}\" data-height=\"{$imageInfo['height']}\"/>" : '';
                $content = str_replace($str,$replace,$content);
            }
        }
        // 是否有视频
        $is_video = 0;

        $id = $ArticleMongoModel->add(array(
            'title'         =>  $title,
            'content'       =>  $content,
            'author'        =>  $author,
            'source_name'   =>  $source_name,
            'source_url'    =>  $source_url,
            'hotnum'        =>  $hotnum,
            'cover'         =>  $cover,
            'cid'           =>  array($cid=>$cid),
            'tags'          =>  $tags,
            'is_image'      =>  $is_image,
            'is_video'      =>  $is_video,
            'publishtime'   =>  intval($publishtime),
            'createtime'    =>  NOW_TIME,
            'updatetime'    =>  NOW_TIME,
            'status'        =>  1,
        ));

        return $id;
    }
}
